<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use DB ;
use App\gen\ModelGenerator;

class GenModel extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'gen:model {table} {className}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生成model';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $table = $this->argument('table') ;
        $colmns = DB::select("SHOW FULL COLUMNS FROM `{$table}`") ;
        ob_start();
        ModelGenerator::build($this,$colmns) ;
        $content = ob_get_contents();
        ob_end_clean();
        $filename = __DIR__.'/../../../app/models/'.$this->argument('className').'.php' ;
        if(!file_exists(dirname($filename))){
            mkdir(dirname($filename),0777,true);
        }
        file_put_contents($filename,$content) ;
    }
}
